package com.vgulu.server.common.bean;

import com.vgulu.server.common.TConstant;
import com.vgulu.server.common.exception.VBaseException;

import java.util.HashMap;
import java.util.List;

/**
 * Created by wangchunsee on 16/10/4.
 */
public class VResponse<T> {
    private int code = 0;
    private String msg = "";
    private T result;


//    //通用错误码
    public static final int SUCCESS = 0;
//    public static final int PARAM_ERROR = 2000;
//
//    public static final VResponse Success = new VResponse(0,"");
//    public static final VResponse InvalidParam =new VResponse(PARAM_ERROR,"无效参数");
//    public static final VResponse UnloginParam =new VResponse(PARAM_ERROR+1,"未登录");
//    public static final VResponse UploadFormatSupport = new VResponse(PARAM_ERROR+2,"上传格式不支持");
//
//    public static final VResponse ServiceException =new VResponse(SERVER_ERROR,"服务器内部异常");


    public VResponse(int errCode, String errMsg) {
        this.code = errCode;
        this.msg = errMsg;
    }

    public VResponse() {

    }

    public String createResponse(){
        return TConstant.gson.toJson(this);
    }

    public static <T> VResponse<T> success(T result){
        VResponse<T> response = new VResponse<>(SUCCESS, null);
        response.result = result;
        return response;
    }

    public static <T> VResponse<T> success(){
        return new VResponse<>(SUCCESS, null);
    }

    public static <T>  VResponse<T> error(String msg){
        return new VResponse<>(VBaseException.SERVER_ERROR, msg);
    }

    public static <T> VResponse<T> error(VBaseException exp){
        return new VResponse<>(exp.getCode(), exp.getMessage());
    }

    public static  <T> VResponse<T> error(Throwable e){
        return new VResponse<>(VBaseException.SERVER_ERROR, e.getMessage());
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }
}
